package graphtheory.岛屿数量;

public class Solution {
    public int numIslands(char[][] grid) {
        int ans = 0 ;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[0].length; j++) {
                if (grid[i][j] == '1'){
                    ans ++ ;
                    dfs(grid , i , j);
                }

            }
        }
        return  ans;
    }

    void dfs(char[][] grid, int r, int c) {
        int rLen = grid.length - 1;
        int cLen = grid[0].length -1 ;
        if (r < 0 || c < 0 || r > rLen || c > cLen || grid[r][c] == '0') {
            return;
        }
        grid[r][c] = '0';
        dfs(grid, r + 1, c);
        dfs(grid, r - 1, c);
        dfs(grid, r, c + 1);
        dfs(grid, r, c - 1);

    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        System.out.println(solution.numIslands(new char[][]{{'1','1','0','0','0'}, {'1','1','0','0','0'}, {'0','0','1','0','0'}, {'0','0','0','1','1'}}));
    }

}
